package com.zhao.service.impl;

import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhao.message.TextMessage;
import com.zhao.service.CoreService;
import com.zhao.util.MessageUtil;


public class CoreServiceImpl implements CoreService{
	Logger log=LoggerFactory.getLogger(CoreServiceImpl.class);
	@Override
	public String processRequest(HttpServletRequest request) {

    	String respXml="";
    	try {
    		Map<String, String> requestMap=MessageUtil.parseXml(request);
    		String fromUserName=requestMap.get("FromUserName");//开发者微信号
    		String toUserName=requestMap.get("ToUserName");//用户openId
    		String msgType=requestMap.get("MsgType");//消息类型
    		
    		log.info("用户openId"+fromUserName);
    		if (msgType.equals("text")) {//文本消息
    			TextMessage message=new TextMessage();
    			message.setToUserName(fromUserName);
        		message.setFromUserName(toUserName);
        		message.setCreateTime(new Date().getTime());
        		message.setMsgType(msgType);
        		message.setContent("很高兴为您服务？");
        		return MessageUtil.messageToXml(message);
			}
    		else if (msgType.equals("image")) {//图片消息
    			return "success";
			} 
			else if (msgType.equals("voice")) {//语音消息（可以关闭此功能）
				return "success";
			} 
			else if (msgType.equals("video")) {//视频
				return "success";
			} 
			else if (msgType.equals("shortvideo")) {//小视频
				return "success";
			} 
			else if (msgType.equals("location")) {//地址位置
				return "success";
			} 
			else if (msgType.equals("link")) {//链接
				
			}else if(msgType.equals("event")) {//事件类型
				String event=requestMap.get("Event");
				log.info("》》事件类型"+event);
				if("subscribe".equals(event)) {//关注
					log.info("》》开始关注");
					TextMessage message=new TextMessage();
	    			message.setToUserName(fromUserName);
	        		message.setFromUserName(toUserName);
	        		message.setCreateTime(new Date().getTime());
	        		message.setMsgType(msgType);
	        		message.setContent("欢迎您的到来！");
	        		return MessageUtil.messageToXml(message);
				}else if("unsubscribe".equals(event)) {//取消关注
					log.info("》》取消关注");
				}else if("SCAN".equals(event)) { 
					log.info("》》已经关注");
					TextMessage message=new TextMessage();
	    			message.setToUserName(fromUserName);
	        		message.setFromUserName(toUserName);
	        		message.setCreateTime(new Date().getTime());
	        		message.setMsgType(msgType);
	        		message.setContent("欢迎您的到来！");
	        		return MessageUtil.messageToXml(message);
				}else {
					return "success";
				}
			}
    		return respXml;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

}
